/**
 * 
 */
package com.ms.service.module.baccarat;

import java.util.HashMap;
import java.util.LinkedHashMap;

import com.ms.service.module.baccarat.bin.BacrBetType;
import com.xpec.c4.message.ConstantCode;

/**
 * 百家樂統計資料
 * 
 * @author TW15010
 */
public class BacrStasticData {
	/**
	 * 勝負組合統計 map
	 * <p>
	 * key: 勝負類型 {@link BacrBetType}<br>
	 * value: 累計次數
	 */
	private LinkedHashMap<Integer, Integer> gameResultStasticMap = new LinkedHashMap<Integer, Integer>();

	/**
	 * 勝負組合統計總次數
	 */
	private int gameResultStasticTotalCount = 0;

	/**
	 * 雙方手牌數量組合統計 map
	 * <p>
	 * key: 莊家手牌張數_閒家手牌張數<br>
	 * value: 出現次數
	 */
	private LinkedHashMap<String, Integer> handCardCountMap = new LinkedHashMap<String, Integer>();

	/**
	 * 雙方手排數量組合統計總次數
	 */
	private int handCardStasticTotalCount = 0;

	/**
	 * 雙方點數組合統計 map
	 * <p>
	 * key: 莊家點數_閒家點數_是否Natural
	 */
	private LinkedHashMap<String, Integer> cardValusCountMap = new LinkedHashMap<String, Integer>();

	/**
	 * 雙方點數組合統計總次數
	 */
	private int cardValueStasticTotalCount = 0;

	/**
	 * 建構
	 */
	public BacrStasticData() {
		// 預先填入所有組合的 key
		for (int bankerCardCount = 2; bankerCardCount <= 3; bankerCardCount++) {
			for (int playerCardCount = 2; playerCardCount <= 3; playerCardCount++) {
				String key = bankerCardCount + "_" + playerCardCount;
				handCardCountMap.put(key, 0);
			}
		}

		// 預先填入所有組合的 key
		for (int natural = 1; natural >= 0; natural--) {
			for (int bankerValue = 0; bankerValue <= 9; bankerValue++) {
				for (int playerValue = 0; playerValue <= 9; playerValue++) {
					String key = bankerValue + "_" + playerValue + "_" + natural;
					cardValusCountMap.put(key, 0);
				}
			}
		}
	}

	/**
	 * 增加勝負結果到統計資料中
	 * 
	 * @param gameResultType
	 *            勝負結果類型 {@link BacrBetType}
	 */
	public void addGameResultStastic(int gameResultType) {
		if (!ConstantCode.isValid(BacrBetType.class, gameResultType)) {
			return;
		}

		int count = 0;

		if (gameResultStasticMap.containsKey(gameResultType)) {
			count = gameResultStasticMap.get(gameResultType);
		}

		count++;
		gameResultStasticMap.put(gameResultType, count);

		if (gameResultType == BacrBetType.BANKER_PAIR || gameResultType == BacrBetType.PLAYER_PAIR) {
			return;
		}

		gameResultStasticTotalCount++;
	}

	public int getGameResultStasticTotalCount() {
		return gameResultStasticTotalCount;
	}

	/**
	 * 取得指定勝負類型的統計次數
	 * 
	 * @param gameResultType
	 *            勝負結果類型 {@link BacrBetType}
	 * @return
	 */
	public int getGameResultStaticCount(int gameResultType) {
		if (!gameResultStasticMap.containsKey(gameResultType)) {
			return 0;
		}

		return gameResultStasticMap.get(gameResultType);
	}

	/**
	 * 更新雙方指定手牌數量組合的出現次數
	 * 
	 * @param bankerCardCount
	 *            莊家手牌數量
	 * @param playerCardCount
	 *            閒家手牌數量
	 */
	public void addHandCardCountStastic(int bankerCardCount, int playerCardCount) {
		String key = bankerCardCount + "_" + playerCardCount;

		if (!handCardCountMap.containsKey(key)) {
			return;
		}

		int count = handCardCountMap.get(key);

		count++;

		handCardCountMap.put(key, count);

		handCardStasticTotalCount++;
	}

	public HashMap<String, Integer> getHandCardCountMap() {
		return handCardCountMap;
	}

	public int getHandCardStasticTotalCount() {
		return handCardStasticTotalCount;
	}

	/**
	 * 更新雙方牌點組合統計次數
	 * 
	 * @param bankerValue
	 *            莊家牌點
	 * @param playerValue
	 *            閒家牌點
	 * @param natural
	 */
	public void addCardValueStastic(int bankerValue, int playerValue, boolean natural) {
		String key = bankerValue + "_" + playerValue;

		if (natural) {
			key += "_1";
		} else {
			key += "_0";
		}

		if (!cardValusCountMap.containsKey(key)) {
			return;
		}

		int count = cardValusCountMap.get(key);

		count++;

		cardValusCountMap.put(key, count);

		cardValueStasticTotalCount++;
	}

	public HashMap<String, Integer> getCardValusCountMap() {
		return cardValusCountMap;
	}

	public int getCardValueStasticTotalCount() {
		return cardValueStasticTotalCount;
	}
}
